<?php
class MssqlTools{
	
	//数据库配置
	private $dbHost;
	private $dbUserName;
	private $dbUserPwd;
	private $dbName;
	
	public function __construct($dbHost,$dbUserName,$dbUserPwd,$dbName){
	    $this->dbHost = $dbHost;
	    $this->dbUserName = $dbUserName;
	    $this->dbUserPwd = $dbUserPwd;
	    $this->dbName = $dbName;
	}
	
	/**
	 * 创建数据库连接
	 * @param array $dbConf
	 * @return resource
	 */
	public function createConnect(){
		try {
			$conn = mssql_connect($this->dbHost, $this->dbUserName, $this->dbUserPwd);
			if (!$conn) 
				die('数据库连接失败!');
			else 
				mssql_select_db($this->dbName, $conn);
			return $conn;
		} catch (\Exception $e) {
			echo $e->getMessage();
		}
	}
	
	/**
	 * 关闭数据库连接
	 * @param resource $conn
	 */
	public function closeConnect($conn){
		mssql_close($conn);
	}
	
	/**
	 * 查询语句
	 * @param string $sql
	 * @param array $condition
	 * @return array
	 */
	public function selectSql($sql){
	    ChromePhp::log('select:'.$sql);
	    try {
	        //装入执行的sql
	        $result = mssql_query($sql);
	        //如果查询失败就直接返回布尔结果
	        if (is_bool($result))
	            return $result;
	        else{
	            $arrayData = array();
	            //使用一个数组保存数据
	            while ($row = mssql_fetch_array($result)) {		
	                array_push($arrayData, $row);
	            }
	            	
	            mssql_free_result($result);
	            return $arrayData;
	        }
	    } catch (Exception $e) {
	        ChromePhp::error($e->getMessage());
	    }
	}
	
	/**
	 * 修改语句
	 * @param string $sql
	 * @param array $condtion 查询条件集合
	 * @param array $sets 修改参数集合
	 * @return num
	 */
	public function executeSql($sql){
	    ChromePhp::log('execute:'.$sql);
	    try {
	        //装入执行的sql
	        $result = mssql_query($sql);
	    } catch (Exception $e) {
	        ChromePhp::error($e->getMessage());
	    }
		
		return $result;
	}
	
	/**
	 * 开启事务
	 */
	public function startTransaction(){
		mssql_query('BEGIN TRANSACTION DEPS02_DEL');
	}
	
	/**
	 * 提交事务
	 */
	public function commitTransaction(){
		mysql_query('COMMIT TRANSACTION DEPS02_DEL');
	}
	
	/**
	 * 回滚事务
	 */
	public function rollback(){
		mysql_query('ROLLBACK TRANSACTION DEPS02_DEL');
	}
	
	/**
	 * 检查传入的参数是否安全
	 * @param string $value
	 * @return string
	 */
	public function check_input($value) {
	    // 去除斜杠
	    if (get_magic_quotes_gpc()) {
	        $value = stripslashes($value);
	    }
	    
	    if(is_array($value)) {
	        foreach($value as $key => $val) {
	            $value[$key] = $this->check_input($val);
	        }
	    } else {
	        // 如果不是数字则加引号
	        if (!is_numeric($value) && !$value=='')
	            $value = "'" . mysql_real_escape_string($value) . "'";
	    }
	    return $value;
	}
	
	/**
	 * 码表数据查询通用方法
	 */
	public function code_query_common($tableName){
	    global $dbHost,	$dbUserName,$dbUserPwd,$dbName;
	    $mssqlTools = new MssqlTools($dbHost,$dbUserName,$dbUserPwd,$dbName);
	    $conn = $mssqlTools->createConnect();
	    $codeList = $mssqlTools->selectSql("
	        SELECT
            	id,
            	name,
            	c.`desc`,
            	isUsed,
            	c.`order`
            FROM
            	$tableName c    
	    ");
	    
	    mssql_close($conn);
	    return $codeList;
	}
	
}
?>